//获取get参数
import { getProgramInfo, getTemplateInfo } from '@/api';

export const getSearchParams = (key) => {
  let params = new URLSearchParams(window.location.search);
  return params.get(key) || '';
};

export const loadViewData = async (programId) => {
  const res = await getProgramInfo({ programId });
  if (res.status !== 0) return;

  const items = res.data.items || [];
  const templateId = res.data.templateId;

  const rs = await getTemplateInfo({ templateId });
  if (rs.status !== 0) return;

  return dealTplData(items, rs.data.regions);
};
const dealTplData = (items, regions) => {
  const regionMap = regions.reduce((map, region) => {
    map[region.regionId] = region;
    return map;
  }, {});

  const jsonData = {};
  items.forEach((item) => {
    const region = regionMap[item.regionId];
    if (region) {
      dealRegionData(jsonData, region.regionDivKey, region.regionType, item.resources);
    }
  });
  return jsonData;
};
const dealRegionData = (jsonData, key, regionType, _resourceArr) => {
  switch (regionType) {
    case 100:
      jsonData[key] = JSON.parse(_resourceArr[0]['attributeDescription']);
      break;
    case 200:
    case 201:
      jsonData[key] = _resourceArr[0]['url'];
      break;
    default:
      jsonData[key] = _resourceArr;
  }
};

export const classOption = (step = 1, limitMoveNum = 5) => {
  return {
    autoPlay: true,
    step: step, // 数值越大速度滚动越快
    openTouch: false,
    limitMoveNum: limitMoveNum, // 开始无缝滚动的数据量
    direction: 1 // 0向下 1向上 2向左 3向右
  };
};
export const docBaseInfo = () => {
  return {
    isPause: false,
    workingPicture: '',
    doctorName: '',
    doctorTitleName: '',
    doctorId: '',
    desc: '',
    registerIdShow: '1',
    queueId: '',
    curr: {},
    wait: []
  };
};
/**
 * @description: 截取数组，截取最大长度，并补齐空数据
 * @param arr 要修改的数据
 * @param length 最大长度
 * @param options {{freeze?: boolean,bool?: boolean,max?: number}}
 * @author: lhf
 * **/
export const handlerLimited = (arr = [], length = 200, options = {}) => {
  if (!arr) {
    return [];
  }
  let fill = [];
  let { bool = false, freeze = false, max = 20 } = options;
  let rs = arr.slice(0, length);
  // 是否需要补全
  if (bool) {
    // 如果小于最大长度
    if (rs.length < max) {
      fill = Array.from({ length: max - rs.length }, () => ({}));
    }
  }
  if (freeze) {
    return Object.freeze([...rs, ...fill]);
  } else {
    return [...rs, ...fill];
  }
};
/**
 * @description: 自动补齐数组
 * @param arr 要修改的数据
 * @param pageSize 页最多多少条
 * @param options {{freeze?: boolean,row?: number}}
 *                 是否需要冻结        一行展示多少个
 * @author: lhf
 * **/
export const autoFillData = (arr = [], pageSize = 4, options = {}) => {
  if (!arr) {
    return [];
  }
  let fill = [];
  let lens = arr.length;
  let { freeze = false, row = 4 } = options;
  // 如果小于展示数据
  if (lens < pageSize) {
    fill = Array.from({ length: pageSize - lens }, () => ({}));
  } else {
    // 如果大于展示数据
    let num = lens % row;
    if (num > 0) {
      fill = Array.from({ length: row - num }, () => ({}));
    }
  }
  if (freeze) {
    return Object.freeze([...arr, ...fill]);
  } else {
    return [...arr, ...fill];
  }
};
